package history.hot100.simple;

import history.hot100.TreeNode;

// 543. 二叉树的直径:https://leetcode-cn.com/problems/diameter-of-binary-tree/
// https://leetcode-cn.com/problems/diameter-of-binary-tree/solution/er-cha-shu-de-zhi-jing-by-leetcode-solution/
public class LeetCode_543 {
    int ans;
    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        depth(root);
        return ans - 1;
    }

    private int depth(TreeNode node) {
        if (node == null) return 0;
        int L = depth(node.left);
        int R = depth(node.right);
        ans = Math.max(ans, L+R+1);
        return Math.max(L,R)+1;
    }
}
